/*
 * sfs_func.c
 *
 *  Created on: Mar 21, 2010
 *      Author: Li Liu
 */

#include "blockio.h"
#include "sfstest.h"
#include <stdlib.h>
#include <string.h>
#include <stdio.h>

/* size of blocks on simulated disk */
#define BLKSIZE  128
/* number of blocks on simulated disk */
#define NUMBLKS  512
#define MAX_IO_LENGTH   1024

/*This is where the root block is located*/
#define ROOT_LK_INDEX  2

/*This is the number of files that can be open at one time*/
#define MAX_OPEN_FILES	20

/*In this design*/
#define NUM_CONTENT_IN_FOLDER  14

static super_blcok SB;
//Initializing the super_block
static int open_file_list[MAX_OPEN_FILES];
//static int open_folder_list[MAX_OPEN_FILES];

static i_blcok open_i_blcok[MAX_OPEN_FILES];
//static i_floder_blcok open_i_floder_blcok[MAX_OPEN_FILES];

static int number_file_open;

int sfs_remove_entry(floder_blcok* fb, char* filename);

int sfs_initialize(int erase) {
    int i,j,k;
    floder_blcok root_blk;
    char temp_block[128];
    printf("Initializing the file system with the option 0\n");
    number_file_open = 0;

    /*Initializing the open_i_blcok*/
    for (k=0;k<MAX_OPEN_FILES;k++){
        open_i_blcok[k].valid=0;
        open_i_blcok[k].block_index=0;
    }

    switch (erase) {
        case 0:
            printf("Initializing the file system with the option 0\n");
            printf("Trying to load the file structure.\n");
            get_block(0, SB.super_block_0);
            get_block(1, SB.super_block_1);
            //printf("1st byte %x\n", SB.super_block_0[0]);

            return 1;
        case 1:
            printf("Initializing the file system with the option 1\n");
            printf("Trying to recreate the file system.\n");
            for (i = 0; i < BLKSIZE; i++) {
                if (i == 0) {
                    SB.super_block_0[i] = 3;
                    printf("1st byte %x\n", SB.super_block_0[0]);
                } else if(i == 1){
                    SB.super_block_0[i] = 2;
                }
                else SB.super_block_0[i] = 0;
                SB.super_block_1[i] = 0;
            }
            root_blk.type = 1;
            root_blk.valid = 1;
            //strcpy(&(root_blk.name_list[0][0]), "test");
            for (i = 0; i < NUM_CONTENT_IN_FOLDER; i++) {
                root_blk.dest_list[i] = 0;
                root_blk.name_list[i][0]='\0';
            }
            root_blk.dest_list[0] = ROOT_LK_INDEX + 1;

            put_block(0, SB.super_block_0);
            put_block(1, SB.super_block_1);


            put_block(ROOT_LK_INDEX, (char*) & root_blk);
            for (i=ROOT_LK_INDEX+1;i<NUMBLKS;i++){
                get_block(i, temp_block);
                for (j=0;j<BLKSIZE;j++){
                    temp_block[j]=0;
                }
                put_block(i, temp_block);
            }
            sfs_list_next_level(&root_blk);
            return 1;
    }

}

int sfs_open(char *pathname) {
    
    int i,j;
    char temp_f_blk[128];
    data_blcok * temp_d_blk;
    char *path_name_ptr = pathname;
    char file_name_buf[7];
    int buf_index = 0;
    int block_index;
    //printf("The input Path name is :%s\n", pathname);
    //printf("First Char is: %d\n", *path_name_ptr);
    if (*path_name_ptr != '/') {
        printf("The path name should start with / .\n");
        return -1;
    }
    if (*(path_name_ptr + 1) == '\0') {
        //root directory
        if (get_block(ROOT_LK_INDEX, open_i_blcok[number_file_open].block_content) != 0) return -1;
        for (j = 0; j < MAX_OPEN_FILES; j++) {
            if (!open_i_blcok[j].valid) break;
        }
        if (j == MAX_OPEN_FILES) return -1;
        open_i_blcok[j].block_index = ROOT_LK_INDEX;
        open_i_blcok[j].file_ptr = 0;
        open_i_blcok[j].type = (open_i_blcok[number_file_open].block_content)[0];
        printf("%d\n", open_i_blcok[j].type);
        open_i_blcok[j].valid = 1;
        //if (temp_d_blk)
        return j;
    }
    block_index = ROOT_LK_INDEX;
    //path_name_ptr++;
    while (*path_name_ptr != '\0') {
        path_name_ptr++;
        if (buf_index < 7) {
            if (*path_name_ptr == '/') {
                if (*(path_name_ptr + 1) == '\0') {
                    printf("You need to specify a file name not a folder name at the end\n");
                    return -1;
                }
                //Just found a directory
                file_name_buf[buf_index] = '\0';

                //if(get_block(block_index, (char*) temp_f_blk)!=0) return -1;
                get_block(block_index, temp_f_blk);
                block_index = sfs_find_next_level((floder_blcok*) temp_f_blk, file_name_buf);

                if (block_index > 0) {
                } else {
                    printf("Invalid path name!!\n");
                    return -1;
                }
                buf_index = 0;
            } else if (*path_name_ptr == '\0') {
                file_name_buf[buf_index] = '\0';
                if (get_block(block_index, temp_f_blk)!=0) return -1 ;
                block_index = sfs_find_next_level((floder_blcok*) temp_f_blk, file_name_buf);        
                if (block_index > 0) {
                    printf("The spcified file exists\n");

                    if (number_file_open == MAX_OPEN_FILES) {
                        printf("Can not open more files on the system\n");
                        return -1;
                    } else {
                        
                        //Has the file and the file can be open, return the discripter
                        number_file_open++;
                        open_file_list[number_file_open] = block_index;
                        if(get_block(block_index, open_i_blcok[number_file_open].block_content)<0){
                            return -1;
                        }
                        printf("size of the file is %d", ((data_blcok*)(open_i_blcok[number_file_open].block_content))->file_size);
                        for (j=0;j<MAX_OPEN_FILES;j++){
                            if (!open_i_blcok[j].valid) break;
                        }
                        if (j==MAX_OPEN_FILES) return -1;
                        open_i_blcok[j].block_index=block_index;
                        open_i_blcok[j].file_ptr=0;
                        open_i_blcok[j].type=(open_i_blcok[number_file_open].block_content)[0];
                        printf("%d\n",open_i_blcok[j].type);
                        open_i_blcok[j].valid=1;
                        //if (temp_d_blk)
                        return j;
                    }
                } else {
                    printf("Sorry, the file you specified does not exist!");
                    return -1;
                }
                buf_index = 0;
            } else {
                file_name_buf[buf_index] = *path_name_ptr;
                buf_index++;
            }
        } else {
            buf_index = 0;
            printf("File names in the path should not be more than 6 chars!.\n");
            return -1;
        }
    }
};

int sfs_read(int fd, int start, int length, char *mem_pointer) {
    int length_to_read=length;
    char *mem_ptr=mem_pointer;
    data_blcok * data_blcok_tmp;
    int next_blk;
    open_i_blcok[fd].file_ptr=start;
    if(open_i_blcok[fd].type) {
        printf("Can't read from a folder file\n");
        return -1;
    }

    while (open_i_blcok[fd].file_ptr>=120){
        open_i_blcok[fd].file_ptr=open_i_blcok[fd].file_ptr%120;
        next_blk=((data_blcok*)(open_i_blcok[fd].block_content))->next_block_num;
        if(get_block(next_blk, open_i_blcok[fd].block_content)!=0) return -1;
    }
    printf("open_i_blcok[fd].block_index: %d\n", open_i_blcok[fd].block_index);
    while (length_to_read){

        if(get_block(open_i_blcok[fd].block_index, open_i_blcok[fd].block_content)!=0) return -1;
        if(open_i_blcok[fd].file_ptr==120){
            //Current Block has been finished for reading
            //Going to the next one
            open_i_blcok[fd].file_ptr=0;
            if (((data_blcok*)(open_i_blcok[fd].block_content))->next_block_num<=0) return -1;
            if(get_block(((data_blcok*)(open_i_blcok[fd].block_content))->next_block_num, open_i_blcok[fd].block_content)!=0) return -1;
        }
        data_blcok_tmp=(data_blcok*)(open_i_blcok[fd].block_content);
        *mem_ptr=((data_blcok*)(open_i_blcok[fd].block_content))->content[open_i_blcok[fd].file_ptr];
        open_i_blcok[fd].file_ptr++;
        //putchar(*mem_ptr);
        mem_ptr++;
        length_to_read--;
    }
    return 1;
};

int sfs_write(int fd, int start, int length, char *mem_pointer) {
    char temp_blk[128];
    int length_to_write=length;
    int working_blk=open_i_blcok[fd].block_index;
    char *mem_ptr=mem_pointer;
    int new_size=((data_blcok*)(open_i_blcok[fd].block_content))->file_size;
    //open_i_blcok[fd].file_ptr=start;
    if(open_i_blcok[fd].type) {
        printf("Can't write to a folder file\n");
        return -1;
    }
    if (start<0){
        open_i_blcok[fd].file_ptr=new_size;
        new_size=new_size+length;
        ((data_blcok*)(open_i_blcok[fd].block_content))->file_size=new_size;
    }else{
        open_i_blcok[fd].file_ptr=start;
        if((start+length)>new_size)
            return -1;
    }

    while (open_i_blcok[fd].file_ptr>=120){
        open_i_blcok[fd].file_ptr=open_i_blcok[fd].file_ptr%120;
        if(get_block(((data_blcok*)(open_i_blcok[fd].block_content))->next_block_num, open_i_blcok[fd].block_content)!=0) return -1;
    }
    while(length_to_write){
        if(open_i_blcok[fd].file_ptr==120){
            if(put_block(working_blk, open_i_blcok[fd].block_content)!=0) return -1;
            open_i_blcok[fd].file_ptr=0;
            if (((data_blcok*)(open_i_blcok[fd].block_content))->next_block_num<=0){
                ((data_blcok*)(open_i_blcok[fd].block_content))->next_block_num=find_first_empty_block();
                ((data_blcok*)temp_blk)->next_block_num=0;
                ((data_blcok*)temp_blk)->type=0;
                ((data_blcok*)temp_blk)->valid=1;
                put_block(((data_blcok*)(open_i_blcok[fd].block_content))->next_block_num, temp_blk);
            }else{
                working_blk=((data_blcok*)(open_i_blcok[fd].block_content))->next_block_num;
            }
            
            if(get_block(((data_blcok*)(open_i_blcok[fd].block_content))->next_block_num, open_i_blcok[fd].block_content)!=0) return -1;
        }
        ((data_blcok*)(open_i_blcok[fd].block_content))->content[open_i_blcok[fd].file_ptr]=*mem_ptr;
        
        putchar(((data_blcok*)(open_i_blcok[fd].block_content))->content[open_i_blcok[fd].file_ptr]);
        open_i_blcok[fd].file_ptr++;
        mem_ptr++;
        length_to_write--;
    }
    if(put_block(working_blk, open_i_blcok[fd].block_content)!=0) return -1;
    return 1;
};

int sfs_readdir(int fd, char *mem_pointer) {
    int i;
    floder_blcok* temp_f_blk;
    char temp[128];
    if(get_block(open_i_blcok[fd].block_index,temp)!=0) return -1;
    temp_f_blk=(floder_blcok*)temp;

    if(open_i_blcok[fd].type){
        for (i=open_i_blcok[fd].file_ptr;i<NUM_CONTENT_IN_FOLDER;i++){
            if (i==0) i++;
            if(temp_f_blk->dest_list[i]>0){
                strcpy(mem_pointer, temp_f_blk->name_list[i]);
                printf("mem_pointer: %s\n",mem_pointer);
                open_i_blcok[fd].file_ptr=i+1;
                return 1;
            }
        }
            
        if (i==NUM_CONTENT_IN_FOLDER)
            return -1;
    }else{
        return -1;
    }
};

int sfs_close(int fd) {
    if(open_i_blcok[fd].valid){
        open_i_blcok[fd].valid=0;
        return 1;
    }else{
        return -1;
    }
};

int sfs_delete(char *pathname) {
    floder_blcok temp_f_blk_ins;
    floder_blcok* temp_f_blk=&temp_f_blk_ins;
    char *path_name_ptr = pathname;
    int block_index;
    int block_index_buf;
    int buf_index = 0;
    char file_name_buf[7];

    if (*path_name_ptr != '/') {
        printf("The path name should start with / .\n");
        return -1;
    }
    block_index = ROOT_LK_INDEX;
    block_index_buf = ROOT_LK_INDEX;
    while (*path_name_ptr != '\0') {
        path_name_ptr++;
        if (buf_index < 7) {
            if (*path_name_ptr == '/') {
                if (*(path_name_ptr + 1) == '\0') {
                    printf("Removing a folder\n");
                    file_name_buf[buf_index] = '\0';
                    if(strlen(file_name_buf)==0) return -1; //In case user remove the root folder

                    block_index = sfs_remove_entry((floder_blcok*) temp_f_blk, file_name_buf);
                    if (block_index > 0) {
                        printf("Removed a file\n");
                        put_block(block_index_buf, (char*) temp_f_blk);
                        clear_supper_block(block_index);
                    }else{
                        printf("The file path dose not exist!\n");
                        return -1;
                    }

                }else{
                    file_name_buf[buf_index] = '\0';
                    if (strlen(file_name_buf) > 6) {
                        printf("The folder name can not exceed 6 chars!!\0");
                        return -1;
                    }
                    get_block(block_index, (char*) temp_f_blk);
                    block_index = sfs_find_next_level((floder_blcok*) temp_f_blk, file_name_buf);
                    if (block_index > 0) {
                        printf("Going into next level of directory and the block number to the next level is %d\n", block_index);
                    }else{
                        printf("The folder path dose not exist!\n");
                        return -1;
                    }
                    block_index_buf = sfs_find_next_level((floder_blcok*) temp_f_blk, file_name_buf);
                    buf_index = 0;
                }
            }else if (*path_name_ptr == '\0') {
                file_name_buf[buf_index] = '\0';
                //printf("File Name is: %s\n",file_name_buf);
                get_block(block_index, (char*) temp_f_blk);
                block_index = sfs_remove_entry((floder_blcok*) temp_f_blk, file_name_buf);
                if (block_index > 0) {
                    printf("Removd a file\n");
                    put_block(block_index_buf, (char*) temp_f_blk);
                    clear_supper_block(block_index);
                }else{
                    printf("The file path dose not exist!\n");
                    return -1;
                }
            }else {
                file_name_buf[buf_index] = *path_name_ptr;
                buf_index++;
            }
        }else {
            buf_index = 0;
            printf("File names in the path should not be more than 6 chars!.\n");
            return -1;
        }
    }

};

int sfs_create(char *pathname, int type) {
    floder_blcok temp_f_blk_ins;
    floder_blcok* temp_f_blk=&temp_f_blk_ins;

    int i;
    data_blcok * temp_d_blk;
    char *path_name_ptr = pathname;
    char file_name_buf[7];
    int buf_index = 0;
    int block_index;
    int block_index_buf;
    //printf("The input Path name is :%s\n", pathname);
    //printf("First Char is: %d\n", *path_name_ptr);
    if (*path_name_ptr != '/') {
        printf("The path name should start with / .\n");
        return -1;
    }

    block_index = ROOT_LK_INDEX;
    block_index_buf = ROOT_LK_INDEX;
    //path_name_ptr++;
    while (*path_name_ptr != '\0') {
        path_name_ptr++;
        if (buf_index < 7) {
            if (*path_name_ptr == '/') {
                if (*(path_name_ptr + 1) == '\0') {
                    printf("You need to specify a file name not a folder name at the end\n");
                    return -1;
                }
                //Just found a directory
                file_name_buf[buf_index] = '\0';
                //printf("Directory Name is: %s\n",file_name_buf);
                if (strlen(file_name_buf) > 6) {
                    printf("The folder name can not exceed 6 chars!!\0");
                    return -1;
                }
                get_block(block_index, (char*) temp_f_blk);
                block_index = sfs_find_next_level((floder_blcok*) temp_f_blk, file_name_buf);
                if (block_index > 0) {
                    printf("Going into next level of directory and the block number to the next level is %d\n", block_index);
                } else {
                    printf("The folder path dose not exist oringially, will create it for you!\n");
                    //Here is checking all the contents in the folder to find a empty slot
                    for (i = 1; i < NUM_CONTENT_IN_FOLDER; i++) {
                        if (temp_f_blk->dest_list[i] == 0) {
                            
                            strcpy(temp_f_blk->name_list[i], file_name_buf);
                            /*Waiting for the folder creating code here*/
                            printf("Placing it at the location: %d\n",find_first_empty_block());
                            temp_f_blk->dest_list[i] = find_first_empty_block();
                            if (create_folder_block(temp_f_blk->dest_list[i], file_name_buf)==0){
                                put_block(block_index_buf, (char*) temp_f_blk);
                            }
                            break;
                        }
                    }

                    if (i == NUM_CONTENT_IN_FOLDER) {
                        printf("The folder is full, couldn't create more folders under it.\n");
                        return -1;
                    }
                }
                block_index_buf = sfs_find_next_level((floder_blcok*) temp_f_blk, file_name_buf);
                buf_index = 0;
            } else if (*path_name_ptr == '\0') {
                file_name_buf[buf_index] = '\0';
                //printf("File Name is: %s\n",file_name_buf);
                get_block(block_index, (char*) temp_f_blk);
                block_index = sfs_find_next_level((floder_blcok*) temp_f_blk, file_name_buf);
                if (block_index > 0) {
                    printf("The file already exists!!\n");
                    return -1;
                } else {
                    for (i = 1; i < NUM_CONTENT_IN_FOLDER; i++) {
                        if (temp_f_blk->dest_list[i] == 0) {
                            //printf("size: %s\n", sizeof(temp_f_blk->name_list[i]));

                            strcpy(temp_f_blk->name_list[i], file_name_buf);
                            printf("Placing it at the location: %d\n",find_first_empty_block());
                            temp_f_blk->dest_list[i] = find_first_empty_block();
                            if (!type)
                                if (create_file_block(temp_f_blk->dest_list[i], file_name_buf)==0){
                                    put_block(block_index_buf, (char*) temp_f_blk);
                                    return 1;
                                }
                                else return 0;
                            else
                                if (create_folder_block(temp_f_blk->dest_list[i], file_name_buf)==0){
                                    put_block(block_index_buf, (char*) temp_f_blk);
                                    return 1;
                                }
                                else return 0;
                            break;
                        }
                    }
                    if (i == NUM_CONTENT_IN_FOLDER) {
                        printf("The folder is full, couldn't create more folders under it.\n");
                        return -1;
                    }
                }

                buf_index = 0;
                break;
            } else {
                file_name_buf[buf_index] = *path_name_ptr;
                buf_index++;
            }
        } else {
            buf_index = 0;
            printf("File names in the path should not be more than 6 chars!.\n");
            return -1;
        }
    }


};

int sfs_getsize(char *pathname) {
    floder_blcok temp_f_blk_ins;
    floder_blcok* temp_f_blk=&temp_f_blk_ins;
    char *path_name_ptr = pathname;
    int block_index;
    int block_index_buf;
    int buf_index = 0;
    char file_name_buf[7];

    if (*path_name_ptr != '/') {
        printf("The path name should start with / .\n");
        return -1;
    }
    block_index = ROOT_LK_INDEX;
    block_index_buf = ROOT_LK_INDEX;
    while (*path_name_ptr != '\0') {
        path_name_ptr++;
        if (buf_index < 7) {
            if (*path_name_ptr == '/') {
                if (*(path_name_ptr + 1) == '\0') {
                    printf("Should not end with / \n");
                    return -1;
                }else{
                    file_name_buf[buf_index] = '\0';
                    if (strlen(file_name_buf) > 6) {
                        printf("The folder name can not exceed 6 chars!!\0");
                        return -1;
                    }
                    get_block(block_index, (char*) temp_f_blk);
                    block_index = sfs_find_next_level((floder_blcok*) temp_f_blk, file_name_buf);
                    if (block_index > 0) {
                        printf("Going into next level of directory and the block number to the next level is %d\n", block_index);
                    }else{
                        printf("The folder path dose not exist!\n");
                        return -1;
                    }
                    block_index_buf = sfs_find_next_level((floder_blcok*) temp_f_blk, file_name_buf);
                    buf_index = 0;
                }
            }else if (*path_name_ptr == '\0') {
                file_name_buf[buf_index] = '\0';
                //printf("File Name is: %s\n",file_name_buf);
                get_block(block_index, (char*) temp_f_blk);
                block_index = sfs_find_next_level((floder_blcok*) temp_f_blk, file_name_buf);
                if (block_index > 0) {


                    /*Waiting for logic to handle finding the file size*/

                    if(get_block(block_index, (char*)temp_f_blk)<0){
                        return -1;
                    }
                    return ((char*)temp_f_blk)[0];

                }else{
                    printf("The file path dose not exist!\n");
                    return -1;
                }
            }else {
                file_name_buf[buf_index] = *path_name_ptr;
                buf_index++;
            }
        }else {
            buf_index = 0;
            printf("File names in the path should not be more than 6 chars!.\n");
            return -1;
        }
    }

};

int sfs_gettype(char *pathname) {
    floder_blcok temp_f_blk_ins;
    floder_blcok* temp_f_blk=&temp_f_blk_ins;
    char *path_name_ptr = pathname;
    int block_index;
    int block_index_buf;
    int buf_index = 0;
    char file_name_buf[7];

    if (*path_name_ptr != '/') {
        printf("The path name should start with / .\n");
        return -1;
    }
    block_index = ROOT_LK_INDEX;
    block_index_buf = ROOT_LK_INDEX;
    while (*path_name_ptr != '\0') {
        path_name_ptr++;
        if (buf_index < 7) {
            if (*path_name_ptr == '/') {
                if (*(path_name_ptr + 1) == '\0') {
                    printf("Should not end with / \n");
                    return -1;
                }else{
                    file_name_buf[buf_index] = '\0';
                    if (strlen(file_name_buf) > 6) {
                        printf("The folder name can not exceed 6 chars!!\0");
                        return -1;
                    }
                    get_block(block_index, (char*) temp_f_blk);
                    block_index = sfs_find_next_level((floder_blcok*) temp_f_blk, file_name_buf);
                    if (block_index > 0) {
                        printf("Going into next level of directory and the block number to the next level is %d\n", block_index);
                    }else{
                        printf("The folder path dose not exist!\n");
                        return -1;
                    }
                    block_index_buf = sfs_find_next_level((floder_blcok*) temp_f_blk, file_name_buf);
                    buf_index = 0;
                }
            }else if (*path_name_ptr == '\0') {
                file_name_buf[buf_index] = '\0';
                //printf("File Name is: %s\n",file_name_buf);
                get_block(block_index, (char*) temp_f_blk);
                block_index = sfs_find_next_level((floder_blcok*) temp_f_blk, file_name_buf);
                if (block_index > 0) {
                    if(get_block(block_index, (char*)temp_f_blk)<0){
                        return -1;
                    }
                    return ((char*)temp_f_blk)[0];

                }else{
                    printf("The file path dose not exist!\n");
                    return -1;
                }
            }else {
                file_name_buf[buf_index] = *path_name_ptr;
                buf_index++;
            }
        }else {
            buf_index = 0;
            printf("File names in the path should not be more than 6 chars!.\n");
            return -1;
        }
    }

};

int sfs_find_next_level(floder_blcok* fb, char* filename) {
    int i;
    int file_dest_ptr = 0;
    for (i = 0; i < NUM_CONTENT_IN_FOLDER; i++) {
        //printf("Comparing %s\n", &fb->name_list[i][0]);
        if (strcmp(&fb->name_list[i][0], filename) == 0) {
            //printf("Passed %s, %s, %d\n", &fb->name_list[i][0],filename, i);
            file_dest_ptr = fb->dest_list[i];
            break;
        }
    }

    return file_dest_ptr;

};
int sfs_remove_entry(floder_blcok* fb, char* filename) {
    int i,k;
    int file_dest_ptr = 0;
    char temp[128];
    floder_blcok * temp_f;
    for (i = 0; i < NUM_CONTENT_IN_FOLDER; i++) {
        //printf("Comparing %s\n", &fb->name_list[i][0]);
        if (strcmp(&fb->name_list[i][0], filename) == 0) {
            //printf("Passed %s, %s, %d\n", &fb->name_list[i][0],filename, i);
            get_block(fb->dest_list[i], temp);
            temp_f=(floder_blcok*)temp;
            if(temp_f->type){
                for(k=0;k<NUM_CONTENT_IN_FOLDER;k++){
                    printf("The Removing folder is not empty\n");
                    if(temp_f->dest_list[k]) return -1;
                }

            }

            file_dest_ptr = fb->dest_list[i];
            fb->dest_list[i]=0;
            fb->name_list[i][0]='\0';
            fb->valid=0;
            break;
        }
    }

    return file_dest_ptr;

};


int sfs_list_next_level(floder_blcok* fb) {
    int i;
    int file_dest_ptr = 0;
    for (i = 0; i < NUM_CONTENT_IN_FOLDER; i++) {
        if (fb->dest_list[i] > ROOT_LK_INDEX) {
            printf("%s ", fb->name_list[i]);
        }
    }
    printf("\n");
    return file_dest_ptr;

};

int commit_supper_block(int block_num){
    char sb_modify;

    if (block_num<256){
        sb_modify=SB.super_block_0[block_num/2];
        if(!(block_num%2)){
            switch (sb_modify){
                case 0: //00
                    sb_modify=2;
                    break;
                case 1: //01
                    sb_modify=3;
                    break;
                case 2: //10
                    printf("The block has been occupied!\n");
                    return -1;
                    break;
                case 3: //11
                    printf("The block has been occupied!\n");
                    return -1;
                    break;
            }
        }else{
            switch (sb_modify){
                case 0: //00
                    sb_modify=1;
                    break;
                case 1: //01
                    printf("The block has been occupied!\n");
                    return -1;
                    break;
                case 2: //10
                    sb_modify=3;
                    break;
                case 3: //11
                    printf("The block has been occupied!\n");
                    return -1;
                    break;
            }
        }
        SB.super_block_0[block_num/2]=sb_modify;
        return put_block(0, SB.super_block_0);
    }else {
        sb_modify=SB.super_block_1[(block_num-256)/2];
        if(!(block_num%2)){
            switch (sb_modify){
                case 0: //00
                    sb_modify=2;
                    break;
                case 1: //01
                    sb_modify=3;
                    break;
                case 2: //10
                    printf("The block has been occupied!\n");
                    return -1;
                    break;
                case 3: //11
                    printf("The block has been occupied!\n");
                    return -1;
                    break;
            }
        }else{
            switch (sb_modify){
                case 0:
                    sb_modify=1;
                    break;
                case 1:
                    printf("The block has been occupied!\n");
                    return -1;
                    break;
                case 2:
                    sb_modify=3;
                    break;
                case 3:
                    printf("The block has been occupied!\n");
                    return -1;
                    break;
            }
        }
        SB.super_block_1[(block_num-256)/2]=sb_modify;
        return put_block(1, SB.super_block_1);
    }
}
int create_folder_block(int block_num, char * folderName) {
    int i;
    floder_blcok temp_f_blk;
    
    temp_f_blk.type = 1;
    strcpy(temp_f_blk.name_list[0], folderName);
    temp_f_blk.valid = 1;

    for (i = 1; i < NUM_CONTENT_IN_FOLDER; i++) {
        temp_f_blk.dest_list[i] = 0;
        temp_f_blk.name_list[i][0]='\0';
    }
    if (put_block(block_num, (char*) &temp_f_blk)==0)
        return commit_supper_block(block_num);
    else
        return -1;

};

int create_file_block(int block_num, char * fileName) {
    data_blcok temp_d_blk;
    temp_d_blk.type = 0;
    temp_d_blk.valid = 1;
    temp_d_blk.file_size=0;
    temp_d_blk.next_block_num=-1;
    if (put_block(block_num, (char*) &temp_d_blk)==0)
        return commit_supper_block(block_num);
    else
        return -1;
};

/*This will check the super to see where the first avaiable block is */
int find_first_empty_block() {
    int found_index = 0;
    int i, j;
    //searching the first super block fisrt
    for (i = 0; i < BLKSIZE; i++) {
        
        switch (SB.super_block_0[i]) {
            case 0: //00
                found_index = i*2;
                break;
            case 1: //01
                found_index = i*2;
                break;
            case 2: //10
                found_index = i*2+1;
                break;
        }
        if (found_index) break;
    }
    if (!found_index) {
        for (j = 0; j < BLKSIZE; j++) {
            switch (SB.super_block_1[j/2]) {
                case 0:
                    found_index = 256 + j;
                    break;
                case 1:
                    found_index = 256 + j;
                    break;
                case 2:
                    found_index = 256 + j + 1;
                    break;
            }
            if (found_index) break;
        }
    }

    return found_index;

}



int clear_supper_block(int block_num){
    char sb_modify;

    if (block_num<256){
        sb_modify=SB.super_block_0[block_num/2];
        if(!(block_num%2)){
            switch (sb_modify){
                case 0: //00
                    printf("The block is not occupied!\n");
                    break;
                case 1: //01
                    printf("The block is not occupied!\n");
                    break;
                case 2: //10
                    sb_modify=0;
                    return -1;
                    break;
                case 3: //11
                    sb_modify=1;
                    return -1;
                    break;
            }
        }else{
            switch (sb_modify){
                case 0: //00

                    printf("The block is not occupied!\n");
                    break;
                case 1: //01
                    sb_modify=0;
                    return -1;
                    break;
                case 2: //10
                    printf("The block is not occupied!\n");
                    break;
                case 3: //11
                    sb_modify=2;
                    return -1;
                    break;
            }
        }
        SB.super_block_0[block_num/2]=sb_modify;
        return put_block(0, SB.super_block_0);
    }else {
        sb_modify=SB.super_block_1[(block_num-256)/2];
        if(!(block_num%2)){
            switch (sb_modify){
                case 0: //00
                    printf("The block is not occupied!\n");
                    break;
                case 1: //01
                    printf("The block is not occupied!\n");
                    break;
                case 2: //10
                    sb_modify=0;
                    return -1;
                    break;
                case 3: //11
                    sb_modify=1;
                    return -1;
                    break;
            }
        }else{
            switch (sb_modify){
                case 0: //00
                    printf("The block is not occupied!\n");
                    break;
                case 1: //01
                    sb_modify=0;
                    return -1;
                    break;
                case 2: //10
                    printf("The block is not occupied!\n");
                    break;
                case 3: //11
                    sb_modify=2;
                    return -1;
                    break;
            }
        }
        SB.super_block_1[(block_num-256)/2]=sb_modify;
        return put_block(1, SB.super_block_1);
    }
}